<template>
  <div>
    <!-- login部分 -->
    <div style="width:100%;height:250px;position:relative;border-bottom:1px solid #f7f7f7;">
      <div style="width:100%;text-align:center;position:absolute;top:40px;">
        <img src="/static/icon/bannilogo.png" style="width:120px;height:120px;border-radius:50%;" />
      </div>
      <div style="width:100%;text-align:center;position:absolute;top:190px;">
        <text style="font-size:24px;">伴你</text>
      </div>
    </div>
    <!-- 授权 -->
    <div>
      <div style="margin-left:30px;margin-top:20px;">
        <div style="100%">
          <text style="color:#888">授权后该应用将会获取以下权限</text>
        </div>
        <div>
          <text style="color:#888">-获得你的手机号</text>
        </div>
      </div>
      <div style="width:100%;position:absolute;bottom:80px;">
        <div style="text-align:center;margin-left:20%;margin-right:20%">
          <!-- <button
            plain="true"
            style="background:#1AAc19;color:#fff;border:none"
            open-type="getPhoneNumber"
            @getphonenumber="getphonenumber"
          >获取手机号</button>-->

          <button
            open-type="getPhoneNumber"
            @getphonenumber="getphonenumber"
            style="width: 400rpx;line-height: 80rpx;color: #ffffff;text-align: center;border-radius: 100rpx;background: linear-gradient(to right, #f83c57, #ffae6d);margin: 50rpx auto;"
          >获取手机号</button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { user_WXMPDecodePhonenum, wxLogin } from "../../../utils/wxUtil";

export default {
  data() {
    return {
      code: ""
    };
  },
  methods: {
    //授权
    async getphonenumber(e) {
      // console.log("获取用户信息" + JSON.stringify(e));
      console.log("获取手机号-111-->");

      var errMsg = e.mp.detail.errMsg;
      if (errMsg == "getUserInfo:fail auth deny") {
        console.log("登录失败");
      } else {
        let encryptedData = e.mp.detail.encryptedData;
        let iv = e.mp.detail.iv;

        let phoneNum = await user_WXMPDecodePhonenum(
          encryptedData,
          iv,
          this.code,
          this.phoneNum
        );
        console.log("获取手机号--->" + JSON.stringify(phoneNum));
        if (phoneNum) {
          this.$router.push({
            path: "/pages/login/getUserInfo/main",
            query: { phoneNum }
          });
        }
      }
    }
  },
  async beforeMount() {
    this.code = await wxLogin();
  }
};
</script>
<style scoped>
</style>
